package com.phpxiu.app.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.phpxiu.app.config.Constants;

/**
 * @file
 * @brief 自己的状态数据
 * @author 吴金洲
 * @note 自己的状态数据
 *
 */
public class MySelfInfo {
    private static final String TAG = MySelfInfo.class.getSimpleName();
    private SharedPreferences sp;
    private String id;//记录id
    private String userSig;//IM登录签名
    private String nickName;    // 昵称
    private String avatar;      // 头像
    private String sign;      // 签名--个性签名intro
    private String CosSig;//上传文件签名

    private String mobilenum;//手机号码
    private String sex;//性别
    private String address;//地址
    private long money=0;//账户钻石数
    private int lvl=1;//等级
    private String merrage="0";//情感状况 0:保密(默认) 1:单身 2:恋爱中 3:已婚 4:同性
    private int age=3;
    private boolean bLiveAnimator;  // 渐隐动画
    private int id_status;//用户类型主播或成员
    private int myRoomNum = -1;//直播房间号，创建房间时使用

    private String verifyState="0";//认证状态 0未提交审核;1审核中;2审核通过 3审核未通过

    private String lastLocation=null;//位置
    private long locationUpdateTime=0;//位置最近更新时间

    private static MySelfInfo ourInstance = new MySelfInfo();

    public static MySelfInfo getInstance() {
        return ourInstance;
    }

    public MySelfInfo() {

    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserSig() {
        return userSig;
    }

    public void setUserSig(String userSig) {
        this.userSig = userSig;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public int getMyRoomNum() {
        return myRoomNum;
    }

    public void setMyRoomNum(int myRoomNum) {
        this.myRoomNum = myRoomNum;
    }

    public String getCosSig() {
        return CosSig;
    }

    public void setCosSig(String cosSig) {
        CosSig = cosSig;
    }

    public boolean isbLiveAnimator() {
        return bLiveAnimator;
    }

    public void setbLiveAnimator(boolean bLiveAnimator) {
        this.bLiveAnimator = bLiveAnimator;
    }


    public String getMobilenum() {
        return mobilenum;
    }

    public void setMobilenum(String mobilenum) {
        this.mobilenum = mobilenum;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public long getMoney() {
        return money;
    }

    public void setMoney(long money) {
        this.money = money;
    }

    public int getLvl() {
        return lvl;
    }

    public void setLvl(int lvl) {
        this.lvl = lvl;
    }

    public String getMerrage() {
        return merrage;
    }

    public void setMerrage(String merrage) {
        this.merrage = merrage;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getVerifyState() {
        return verifyState;
    }

    public void setVerifyState(String verifyState) {
        this.verifyState = verifyState;
    }

    public String getLastLocation() {
        return lastLocation;
    }

    public void setLastLocation(String lastLocation) {
        this.lastLocation = lastLocation;
    }

    public long getLocationUpdateTime() {
        return locationUpdateTime;
    }

    public void setLocationUpdateTime(long locationUpdateTime) {
        this.locationUpdateTime = locationUpdateTime;
    }

    public void writeToCache(Context context) {
        SharedPreferences settings = context.getSharedPreferences(Constants.USER_INFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(Constants.USER_ID, id);
        editor.putString(Constants.USER_SIG, userSig);
        editor.putString(Constants.USER_NICK, nickName);
        editor.putString(Constants.USER_AVATAR, avatar);
        editor.putString(Constants.USER_SIGN, sign);
        editor.putString(Constants.USER_COS_SIG,CosSig);
        editor.putInt(Constants.USER_ROOM_NUM, myRoomNum);
        editor.putBoolean(Constants.LIVE_ANIMATOR, bLiveAnimator);
        //editor.putInt(Constants.LOG_LEVEL, logLevel.ordinal());
        editor.putString(Constants.USER_PHONE_NUMBER, mobilenum);
        editor.putString(Constants.USER_SEX, sex);
        editor.putString(Constants.USER_ADDRESS, address);
        editor.putLong(Constants.USER_MONEY, money);
        editor.putInt(Constants.USER_LVL, lvl);
        editor.putString(Constants.USER_MARRIAGE, merrage);
        editor.putInt(Constants.USER_AGE, age);
        editor.putString(Constants.USER_VERIFY_STATE, verifyState);
        editor.commit();
    }

    public void clearCache(Context context) {
        SharedPreferences settings = context.getSharedPreferences(Constants.USER_INFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();
    }

    public void getCache(Context context) {
        SharedPreferences sharedata = context.getSharedPreferences(Constants.USER_INFO, Context.MODE_PRIVATE);
        id = sharedata.getString(Constants.USER_ID, null);
        userSig = sharedata.getString(Constants.USER_SIG, null);
        myRoomNum = sharedata.getInt(Constants.USER_ROOM_NUM, -1);
        nickName = sharedata.getString(Constants.USER_NICK, null);
        avatar = sharedata.getString(Constants.USER_AVATAR, null);
        sign = sharedata.getString(Constants.USER_SIGN, null);
        bLiveAnimator = sharedata.getBoolean(Constants.LIVE_ANIMATOR, false);
        mobilenum=sharedata.getString(Constants.USER_PHONE_NUMBER,null);
        sex=sharedata.getString(Constants.USER_SEX,null);
        address=sharedata.getString(Constants.USER_ADDRESS,null);
        money=sharedata.getLong(Constants.USER_MONEY, 0);
        lvl=sharedata.getInt(Constants.USER_LVL, 1);
        merrage=sharedata.getString(Constants.USER_MARRIAGE,"0");
        age=sharedata.getInt(Constants.USER_AGE, 3);
        CosSig=sharedata.getString(Constants.USER_COS_SIG,null);
        verifyState=sharedata.getString(Constants.USER_VERIFY_STATE,"0");
        lastLocation=sharedata.getString(Constants.USER_LAST_LOCATION,null);
        locationUpdateTime=sharedata.getLong(Constants.USER_LAST_LOCATION_UPDATE_TIME,0L);
    }

    public int updateCacheInt(Context context,String key,int newValue){
        SharedPreferences sp=context.getSharedPreferences(Constants.USER_INFO,Context.MODE_PRIVATE);
        sp.edit().putInt(key,newValue).commit();
        updateCacheValue(key,newValue+"");
        return newValue;
    }

    public long updateCacheLong(Context context,String key,long newValue){
        SharedPreferences sp=context.getSharedPreferences(Constants.USER_INFO,Context.MODE_PRIVATE);
        sp.edit().putLong(key,newValue).commit();
        updateCacheValue(key,newValue+"");
        return newValue;
    }

    public String updateCacheString(Context context,String key,String newValue){
        SharedPreferences sp=context.getSharedPreferences(Constants.USER_INFO,Context.MODE_PRIVATE);
        sp.edit().putString(key,newValue).commit();
        updateCacheValue(key,newValue);
        return newValue;
    }

    /**
     * 更新某个内容的缓存
     * @param key
     * @param newValue
     * @return
     */
    public void updateCacheValue(String key,String newValue){
        switch (key){
            case Constants.USER_ID:
                id=newValue;
                break;
            case Constants.USER_SIG:
                userSig=newValue;
                break;
            case Constants.USER_NICK:
                nickName=newValue;
                break;
            case Constants.USER_AVATAR:
                avatar=newValue;
                break;
            case Constants.USER_VERIFY_STATE:
                verifyState=newValue;
                break;
            case Constants.USER_SIGN:
                setCosSig(newValue);
                break;
            case Constants.USER_COS_SIG:
                sign=newValue;
                break;
            case Constants.USER_ROOM_NUM:
                try {
                    myRoomNum=Integer.parseInt(newValue);
                }catch (NumberFormatException e){

                }
                break;
            case Constants.USER_PHONE_NUMBER:
                mobilenum=newValue;
                break;
            case Constants.USER_SEX:
                sex=newValue;
                break;
            case Constants.USER_ADDRESS:
                address=newValue;
                break;
            case Constants.USER_MONEY:
                try {
                    money= Long.parseLong(newValue);
                }catch (NumberFormatException e){
                    money=0;
                }
                break;
            case Constants.USER_LVL:
                try {
                    lvl=Integer.parseInt(newValue);
                }catch (NumberFormatException e){
                    lvl=1;
                }
                break;
            case Constants.USER_MARRIAGE:
                merrage=newValue;
                break;
            case Constants.USER_AGE:
                try {
                    age=Integer.parseInt(newValue);
                }catch (NumberFormatException e){
                    age=3;
                }
                break;
            case Constants.USER_LAST_LOCATION:
                lastLocation=newValue;
                break;
            case Constants.USER_LAST_LOCATION_UPDATE_TIME:
                try {
                    locationUpdateTime=Long.parseLong(newValue);
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 更改钻石数
     * @param increase true增加 false减少
     * @param value 变动数值
     */
    public long updateMoney(Context context,boolean increase,long value){
        if(increase){
            money=money+value;
        }else{
            money=money-value;
            money=money>0?money:0;
        }
        if(sp==null){
            sp=context.getSharedPreferences(Constants.USER_INFO,Context.MODE_PRIVATE);
        }
        sp.edit().putLong(Constants.USER_MONEY,money).commit();
        return money;
    }

    public int getIdStatus() {
        return id_status;
    }

    public void setIdStatus(int id_status) {
        this.id_status = id_status;
    }

    /**
     * 用户登录出
     */
    public void loginOut(){
        id=null;
        userSig=null;
        nickName=null;
        sign=null;
        CosSig=null;
        //创建新的空实例,并指向
        ourInstance=new MySelfInfo();
    }
}